<template>
	<view class="safe-area-inset-bottom">
		<view class="swip_header">
			<view class="slot-box">
				<view style="width: 100%;">
					<uni-search-bar v-model="queryParams.params.contractNameAndContractNumber" type="text" @confirm="handleQuery" @input="handleQuery" placeholder="请输入合同名称 / 合同编号" />
				</view>
			</view>
		</view>
		
		<!-- 列表 -->
		<view v-if="contractList != ''" class="listSearchTop cardList">
		  <uni-card :is-shadow="false" v-for="(item,index) in contractList" :key="index">
			<p class="grid-hidden contract_Name">{{ item.name }}</p>
			<p class="grid-integral">合同编号：{{ item.contractNumber }}</p>
			<p class="grid-integral">业务分类：{{ item.contractTypeName }} {{ item.businessTypeName }}</p>
			<p class="grid-hidden">签约方：{{ item.signName }}</p>
			<p class="grid-integral">经办人：{{item.staffName}}</p>
			<p class="grid-integral">修改时间：{{item.updateTime}}</p>
			<view slot="actions" class="card-actions">
				<view>
					签署状态：
					<template v-for="(dictItem, index) in dicts.ec_contract_status">
					  <template v-if="item.status == dictItem.value">
						<span class="errorColor">{{dictItem.text}}</span>
					  </template>
					</template>
				</view>
				<view class="flex_between">
			  <view class="card-actions-item" @click.stop="handleEdit(item)">
			    <text class="card-actions-item-text themeColor">修改</text>
			  </view>
			  <view class="card-actions-item" @click.stop="handleDelete(item,index)">
			    <text class="card-actions-item-text errorColor">删除</text>
			  </view>
			  </view>
			</view>
		</uni-card>
		<!-- 加载更多 -->
		<uni-load-more color="#00a660" :status="loadStatus"/>
		</view>
		<!-- 暂无数据 -->
		<luanqingEmpty :show="true" text="没有更多数据啦" v-else />
	</view>
</template>

<script>
	import { listContract, delContract, addContract, updateContract } from "@/api/ec/work/contract/contract";
	import luanqingEmpty from '@/components/luanqing-empty/luanqing-empty';
	import {getDictsByNameForUni} from "@/utils/dicts";
	export default {
		components:{luanqingEmpty,},
		data() {
			return {
				// 数据字典
				dicts: {
				  // 签署状态
				  ec_contract_status: [],
				},
				total:0 ,//总条数
				loadStatus:'loading', //是否加载中 //加载样式：more-加载前样式，loading-加载中样式，nomore-没有数据样式
				msgType:'info',
				contractList: [], // 合同类型列表 
				queryParams: {
				        pageNum: 1,
				        pageSize: 10,
				        companyId: null,
				        staffId: null,
				        name: null,
				        status: null,
				        contractNumber: null,
				        contractTypeId: null,
				        businessTypeId: null,
				        params:{
				          contractNameAndContractNumber:null,
				          signCompanyAllName:null,
				        },
				},     
			}
		},
		onLoad() {
			this.getList();
			this.getDicts()
		},
		/** 上拉刷新回调 */
		onReachBottom() {
		  if (this.loadStatus != 'noMore' && this.total > this.contractList.length) {
		    this.loadStatus = 'loading';
		    // 延迟500毫秒显示loading效果
		    setTimeout(() => {
		      // 执行查询的方法
		      this.getList(false);
		    }, 500)
		  } else {
		    // 停止加载
		    this.loadStatus = 'noMore'
		  }
		},
		methods: {
			/**加载数据字典*/
			getDicts() {
			  // 签署状态
			  this.dicts.ec_contract_status = getDictsByNameForUni("ec_contract_status");
			},
			/** 搜索 */
			handleQuery(){
				this.getList(true);      
			},
			/**
			 * 查询数据列表
			 * @param reload true 重新加载数据； false 加载更多
			 */
			getList(reload) {
			  if (reload) {
			    this.queryParams.pageNum = 1;
			    this.loadStatus = 'more'
          this.$modal.loading();
			  }
			  listContract(this.queryParams).then(response => {
			    if (reload) {
			      uni.hideLoading()
			    }
			    this.total = response.total;
			    if (response.total > 0) {
			      const dataRows = response.rows;
			      this.contractList = reload ? dataRows : this.contractList.concat(dataRows);
			    } else {
			      this.contractList = [];
			    }
			    // 判断数据是否加载完全 
			    if (response.total <= this.contractList.length) {
			      this.loadStatus = 'noMore'
			    }
			    // 页数+1 
			    this.queryParams.pageNum++
			  }).catch(() => {
			    if (reload) {
			      uni.hideLoading()
			    }
			  });
			},
			/** 修改 */
			handleEdit(row){
				switch (row.status) {
					case 0:
						this.$tab.reLaunch(`/pages/sign/sign?id=${row.id}&flowId=${''}`)
						break;
					case 1:
						this.$tab.navigateTo(`/pages/sign/signedByme?id=${row.id}&flowId=${''}`)
						break;
					case 2:
						this.$tab.navigateTo(`/pages/sign/docfill?id=${row.id}&flowId=${''}`)
						break;
					case 3:
						this.$tab.navigateTo(`/pages/sign/upSign?id=${row.id}&flowId=${''}`)
						break;
					case 4:
						this.$tab.navigateTo(`/pages/sign/signedByme?id=${row.id}&flowId=${''}`)
						break;
					case 6:
						this.$tab.navigateTo(`/pages/sign/upSign?id=${row.id}&flowId=${''}`)
						break;	
					case 13:
						this.$tab.navigateTo(`/pages/sign/signedByme?id=${row.id}&flowId=${''}`)
						break;		
					default:
						this.$tab.navigateTo(`/pages/sign/detail?id=${row.contractId}&flowId=${row.id}`)
						break;
				}
			},
			/** 删除 */
			handleDelete(row,index){
				var _this = this
				uni.showModal({
					title:'系统提示',
					content:'是否删除合同？',
					confirmColor:'#00a660',
					success:function(res){
						if(res.confirm){
							delContract(row.id)
							_this.$modal.msgSuccess("删除成功");
							_this.contractList.splice(index,1)
						}else{
						}
					}
				})
			}, 
			/** 新建草稿合同 */
			handleAdd(){
				uni.switchTab({
					url:'/pages/sign/sign'
				})
			},
		}
	}
</script>
<style lang="scss" scoped>
	page {
		background:$uni-bg-color-grey;
	}
</style>